<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="deptId" :data="group" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header style="height: auto;">
				<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
			</el-header>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="createUserExpense">创建</el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
				<div class="right-panel">
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="用户账号" prop="accountId" width="150" sortable='custom'></el-table-column>
					<el-table-column label="年度" prop="expenseYear" width="150" sortable='custom'></el-table-column>
					<el-table-column label="用户姓名" prop="userName" width="100"></el-table-column>
					<el-table-column label="性别" prop="sex" width="60">
						<template #default="scope">
							<el-button type="primary" size="small" v-if="scope.row.sex==='1'">男</el-button>
							<el-button type="danger" size="small" v-else-if="scope.row.sex==='0'">女</el-button>
							<el-button type="info" size="small" v-else>保密</el-button>
						</template>
					</el-table-column>
					<el-table-column label="所在部门" prop="deptName" width="100" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="职务" prop="postion" width="100"></el-table-column>
					<el-table-column label="手机号" prop="phone" width="150"></el-table-column>
					<el-table-column label="预算金额" prop="amount" width="150"></el-table-column>
					<el-table-column label="余额" prop="balance" width="150"></el-table-column>
					<el-table-column label="备注" prop="remark" width="100"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="150">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<asave-dialog v-if="dialog.save" ref="saveDialog" @closed="dialog.save=false"  @success="handleSaveSuccess"></asave-dialog>
</template>

<script>
import asaveDialog from './asave'
import scSelectFilter from '@/components/scSelectFilter'
export default {
	name: 'Expense Account Config',
	components: {
		scSelectFilter,
		asaveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			filterData: [
				{
					title: "预算年度",
					key: "expenseYear",
					multiple: false,
					options: [{
						label: "全部",
						value: ""
					},]
				}
			],
			selection: [],
			showGrouploading: false,
			groupFilterText: '',
			group: [],
			groupsProps: {
				label:"name",
				value: "id"
			},
			apiObj: this.$API.finance.expense.getExpenseUserConfigList,
			search: {
				expenseYear:'',
				deptId:'',
				accountId:'',
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getDeptGroup();
		this.getExpenseYear();
	},
	methods: {
		getExpenseYear()
		{
			let yearArr=[];
			let currentYear = parseInt(new Date().getFullYear());
			for(let i=0;i<10;i++)
			{
				let year = currentYear-i;
				let tempJson={
					value:year,
					label:year+"年度"
				};
				yearArr.push(tempJson);
			}
			this.filterData[0].options=this.filterData[0].options.concat(yearArr);
		},
		createUserExpense() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var recordIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					recordIdArr.push(this.selection[i].recordId);
				}
				const loading = this.$loading();
				var res = await this.$API.finance.expense.deleteExpenseUserConfigByIds.post({ids: recordIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		table_edit(row,index){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row)
			})
		},
		async table_del(row) {
			var reqData = {recordId: row.recordId}
			var res = await this.$API.finance.expense.deleteExpenseUserConfig.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//加载树数据
		async getDeptGroup(){
			this.showGrouploading = true;
			var res = await this.$API.system.user.getAllUserForTree.get();
			this.showGrouploading = false;
			var allNode ={id: '', name: '所有'}
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data){
			if (!value) return true;
			return data.name.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data){
			if(data.id=='')
			{
				this.search.deptId="";
				this.search.accountId = "";
			}else{
				if(data.isUser)
				{
					this.search.accountId = data.id
					this.search.deptId="";
				}else{
					this.search.deptId = data.id;
					this.search.accountId = "";
				}
			}
			this.$refs.table.upData(this.search)
		},
		selectionChange(selection) {
			this.selection = selection;
		},
		filterChange(data){
			this.search.expenseYear = data.expenseYear;
			this.$refs.table.upData(this.search)
		},
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSaveSuccess(){
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
